import { useState } from 'react';
import an58 from '@mmstudio/an000058';
import { Message as M1, Result as R1 } from '../../api/admin/role/edit.api';
import api from '../../../atoms/api';
import EditButton from '../../../components/editbutton';
import Form from './form';

export default function Edit({ data }: {
	data: M1;
}) {
	const [d, setd] = useState(data);
	return <>
		<EditButton dlgtitle='修改角色' onShow={() => {
			setd(data);
		}} onSave={async () => {
			if (!d
				|| !d.roleid
				|| !d.name
			) {
				return '缺少必要的参数';
			}
			const res = await an58<R1, M1>(api['/api/admin/role/edit'], 'post', d);
			if (res.ok === true) {
				return true;
			}
			return res.message;
		}} >
			<Form data={d} onChange={(key, val) => {
				setd((pre) => {
					return {
						...pre,
						[key]: val
					};
				});
			}} />
		</EditButton>
	</>;
}
